package zhjb.platform.module.urlcollect.action;

import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.Action;

import net.sf.json.JSONSerializer;
import zhjb.platform.dao.DCriteriaPageSupport;
import zhjb.platform.module.urlcollect.bean.URLType;
import zhjb.platform.module.urlcollect.bean.UrlCollect;
import zhjb.platform.module.urlcollect.service.UrlCollectService;
import zhjb.platform.util.SpringUtil;
import zhjb.platform.web.BaseAction;

public class UrlCollectAction extends BaseAction {
	private String token = null;
	private final static String tokenString = ",9b9547b5c64741ca900f05f94c990f0a,19b7941c1b144199acdc9c87684c8ecd,713174f6b72246f38bcda2d0fc2ad897,e4ad9f18dd9f49a8afc37e6e60a83cd1,b96d83aa4dcf4cfabcfd18b4588202da,04ce43acc062409cbe83ec06462fd8aa,763f79a4cfa34e82998794e9f411d5aa,3b77d841261145f1831d2e4d905db0cd,5977e24479b74a22b54064a54b680e23,b5bde5e73f3e4aad9cadb072956d4426,b74b2ab37a7c4c03a23eea3c053504be,d3fed3f042054642ad4473094deb6061,1ca992044c2b4a4098e93748c1d1541e,";
	private UrlCollectService urlCollectService = (UrlCollectService) SpringUtil.getWebApplicationContext().getBean("urlcollectservice");
	private UrlCollect collect;
	private List<UrlCollect> collectList;
	private String[] urlIds;
	private String query;

	/**
	 * 获取所有URL分类
	 * 
	 * @return
	 */
	public String findAllUrlType() {
		System.out.println(tokenString);
		System.out.println("," + token.toLowerCase() + ",");
		if (tokenString.indexOf("," + token.toLowerCase() + ",") > -1) {
			List<URLType> urlTypes = urlCollectService.findAllURLType();
			String jsonResult = JSONSerializer.toJSON(urlTypes).toString();
			writeString(jsonResult, "html/text", true, false);
		}
		return null;
	}

	/**
	 * 保存数据
	 * 
	 * @return
	 */
	public String addUrlCollect() {
		if (tokenString.indexOf("," + token.toLowerCase() + ",") > -1) {
			UrlCollect has = null;
			String message = "{\"message\":0}";// 失败标示
			if (collect.getCachecontent() == null || collect.getCachecontent().trim().length() == 0) {
				has = urlCollectService.findCollectCount(collect.getUrl(), collect.getUrltype(), "admin");
			}
			if (has == null) {
				collect.setCreatetime(new Date());
				collect.setCreateuser("admin");
				urlCollectService.addCollect(collect);
				message = "{\"message\":1}";
			} else {
				has.setRemark(collect.getRemark());
				has.setCreatetime(new Date());
				urlCollectService.update(has);
				message = "{\"message\":2}";
			}
			writeString(message, "html/text", true, false);
		}
		return null;
	}

	/**
	 * 显示收藏的url
	 * 
	 * @return
	 */
	public String listUrlCollect() {
		pageSize = 25;
		String query = request.getParameter("query");
		collectList = urlCollectService.listUrlCollect(page, pageSize, "admin", query);
		DCriteriaPageSupport<UrlCollect> collectList1 = (DCriteriaPageSupport<UrlCollect>) collectList;
		total = collectList1.getTotalCount();
		return Action.SUCCESS;
	}

	/**
	 * 删除收藏的URL
	 * 
	 * @return
	 */
	public String delete() {
		urlCollectService.delete(urlIds);
		return null;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public UrlCollect getCollect() {
		return collect;
	}

	public void setCollect(UrlCollect collect) {
		this.collect = collect;
	}

	public UrlCollectService getUrlCollectService() {
		return urlCollectService;
	}

	public void setUrlCollectService(UrlCollectService urlCollectService) {
		this.urlCollectService = urlCollectService;
	}

	public List<UrlCollect> getCollectList() {
		return collectList;
	}

	public void setCollectList(List<UrlCollect> collectList) {
		this.collectList = collectList;
	}

	public String[] getUrlIds() {
		return urlIds;
	}

	public void setUrlIds(String[] urlIds) {
		this.urlIds = urlIds;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}
}
